package com.yida.wy.basic.controller.common;

import cn.hutool.core.bean.BeanUtil;
import com.mybatisflex.core.query.QueryWrapper;
import com.yida.common.core.web.domain.AjaxResult;
import com.yida.common.core.web.page.TableDataInfo;
import com.yida.common.log.annotation.Log;
import com.yida.common.log.enums.BusinessType;
import com.yida.common.security.annotation.RequiresPermissions;
import com.yida.wy.basic.domain.WyOwnerAttest;
import com.yida.wy.basic.domain.dto.WyOwnerAttestDetailDTO;
import com.yida.wy.basic.domain.dto.WyOwnerAttestListDTO;
import com.yida.wy.basic.domain.request.WyOwnerAttestApproveRequest;
import com.yida.wy.basic.domain.request.WyOwnerAttestListRequest;
import com.yida.wy.basic.enums.WyOwnerAttestApproveStatus;
import com.yida.wy.basic.service.IWyOwnerAttestService;
import com.yida.wy.common.service.IWyFileService;
import com.yida.wy.common.web.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import static com.yida.wy.basic.domain.table.WyOwnerAttestTableDef.WY_OWNER_ATTEST;

/**
 * @author lqq
 * @date 2023-08-25
 */
@RestController
@RequestMapping("/ownerAttest")
@Api(tags = "业主信息认证")
public class WyOwnerAttestController extends BaseController {

    @Resource
    private IWyOwnerAttestService wYOwnerAttestService;

    @Resource
    private IWyFileService wyFileService;

    @ApiOperation(value = "业主认证申请列表", response = WyOwnerAttestListDTO.class)
    @GetMapping("/list")
    @RequiresPermissions("gwj:basis:ownerAttest:list")
    public TableDataInfo submit(WyOwnerAttestListRequest request) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.where(WY_OWNER_ATTEST.AREA_ID.eq(request.getAreaId()))
                .and(WY_OWNER_ATTEST.HOUSE_LOCATION.like(request.getHouseLocation()))
                .and(WY_OWNER_ATTEST.COMMUNITY_ID.eq(request.getCommunityId()))
                .and(WY_OWNER_ATTEST.COMMUNITY_NAME.like(request.getCommunityName()))
                .and(WY_OWNER_ATTEST.OWNER_NAME.like(request.getOwnerName()))
                .and(WY_OWNER_ATTEST.STATUS.eq(request.getStatus()))
                .and(WY_OWNER_ATTEST.OWNER_PHONE.like(request.getOwnerPhone()));
        return getDataTable(wYOwnerAttestService.basicPageAs(request.getPageNum(), request.getPageSize(), wrapper, WyOwnerAttestListDTO.class));
    }

    @ApiOperation(value = "核对业主信息", response = WyOwnerAttestDetailDTO.class)
    @GetMapping("/{ownerAttestId}")
    @RequiresPermissions("gwj:basis:ownerAttest:query")
    public AjaxResult getInfo(@PathVariable Long ownerAttestId) {
        WyOwnerAttest attest = wYOwnerAttestService.getById(ownerAttestId);
        WyOwnerAttestDetailDTO r = BeanUtil.copyProperties(wYOwnerAttestService.getById(ownerAttestId), WyOwnerAttestDetailDTO.class);
        r.setCertificateImgUrl(wyFileService.getFileInfo(attest.getCertificateImgName(), true).getUrl());
        r.setIdCardAImgUrl(wyFileService.getFileInfo(attest.getIdCardAImgName(), true).getUrl());
        r.setIdCardBImgUrl(wyFileService.getFileInfo(attest.getIdCardBImgName(), true).getUrl());
        return AjaxResult.success(r);
    }

    @Log(title = "业主信息认证-审批通过", businessType = BusinessType.UPDATE, systemName = "gwj")
    @PutMapping(value = "/pass")
    @ApiOperation(value = "业主信息认证-审批通过")
    @RequiresPermissions("gwj:basis:ownerAttest:approve")
    public AjaxResult pass(@RequestBody WyOwnerAttestApproveRequest request) {
        request.setApproveCode(WyOwnerAttestApproveStatus.PASS.getCode());
        wYOwnerAttestService.approve(request);
        return AjaxResult.success("审批成功");
    }

    @Log(title = "业主信息认证-审批不通过", businessType = BusinessType.UPDATE, systemName = "gwj")
    @PutMapping(value = "/notPass")
    @ApiOperation(value = "审批不通过")
    @RequiresPermissions("gwj:basis:ownerAttest:approve")
    public AjaxResult notPass(@RequestBody WyOwnerAttestApproveRequest request) {
        request.setApproveCode(WyOwnerAttestApproveStatus.NOT_PASS.getCode());
        wYOwnerAttestService.approve(request);
        return AjaxResult.success("审批成功");
    }
}
